package cn.ldw.service.impl;

import cn.ldw.mapper.SignInfoMapper;
import cn.ldw.pojo.User;
import cn.ldw.service.SignInfoService;
import cn.ldw.utils.SevenDate;
import cn.ldw.utils.UserThreadLocal;
import cn.ldw.vo.*;
import cn.ldw.pojo.Signinfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SignInfoServiceImpl implements SignInfoService {

    @Autowired(required = false)
    private SignInfoMapper signInfoMapper;

    @Override
    public Result signTotal(String username) {
        User user = UserThreadLocal.get();
        Integer total = 0;
        if (user.getUserRole() == 1) {
            if (username != null) {
                total = signInfoMapper.getTotal(username);
            } else {
                total = signInfoMapper.getTotal("");
            }
        } else {
            total= signInfoMapper.getTotal(user.getUsername());
        }
        return Result.success(total);
    }

    @Override
    public Result punch(PunchRecordParams punchRecordParams) {
        Signinfo signinfo = new Signinfo();
        User user = UserThreadLocal.get();
        signinfo.setUsername(user.getUsername());
        signinfo.setTemperature(punchRecordParams.getTemperature());
        signinfo.setType(punchRecordParams.getType());
        signinfo.setStatus(punchRecordParams.getStatus());
        signinfo.setSelfFever(punchRecordParams.getSelfFever());
        signinfo.setSelfCough(punchRecordParams.getSelfCough());
        signinfo.setPunchDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        signinfo.setFamilyFever(punchRecordParams.getFamilyFever());
        signinfo.setFamilyCough(punchRecordParams.getFamilyCough());
        signinfo.setAddress(punchRecordParams.getAddress());
        signInfoMapper.insert(signinfo);
        return Result.success(null);
    }

    @Override
    public Result record(PageParams params) {
        LambdaQueryWrapper<Signinfo> queryWrapper = new LambdaQueryWrapper<>();
        ArrayList<RecordVo> recordVos = new ArrayList<>();
        User user = UserThreadLocal.get();
        Page<Signinfo> page = new Page<>(params.getCurrentPage(), params.getPageSize());
        if (user.getUserRole() != 1) {
            queryWrapper.eq(Signinfo::getUsername, user.getUsername());
        }
        if (params.getUsername() != null) {
            queryWrapper.eq(Signinfo::getUsername, params.getUsername());
        }
        queryWrapper.select(Signinfo::getUsername, Signinfo::getTemperature, Signinfo::getStatus, Signinfo::getAddress, Signinfo::getPunchDate);
        queryWrapper.orderByDesc(Signinfo::getPunchDate);
        Page<Signinfo> selectPage = signInfoMapper.selectPage(page, queryWrapper);
        List<Signinfo> signinfoList = selectPage.getRecords();
        for (Signinfo signinfo : signinfoList) {
            RecordVo recordVo = new RecordVo();
            BeanUtils.copyProperties(signinfo, recordVo);
            recordVos.add(recordVo);
        }
        return Result.success(recordVos);
    }

    @Override
    public Result recent() {
        User user = UserThreadLocal.get();
        if (user.getUsername() == "ldw") {
            RecordVo recordVo = signInfoMapper.recent("");
            return Result.success(recordVo);
        }
        RecordVo recordVo = signInfoMapper.recent(user.getUsername());
        return Result.success(recordVo);
    }

    @Override
    public Result getName() {
        ArrayList<PunchName> punchNames = new ArrayList<>();
        LambdaQueryWrapper<Signinfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Signinfo::getUsername);
        queryWrapper.groupBy(Signinfo::getUsername);
        List<Signinfo> signinfoList = signInfoMapper.selectList(queryWrapper);
        for (Signinfo signinfo : signinfoList) {
            PunchName punchName = new PunchName();
            BeanUtils.copyProperties(signinfo, punchName);
            punchNames.add(punchName);
        }
        return Result.success(punchNames);
    }

    @Override
    public Result statisticBar() {
        List<StatisticBarVo> statisticBarVoList = signInfoMapper.statisticBar();
        return Result.success(statisticBarVoList);
    }

    @Override
    public Result statisticPie() {
        List<StatisticPieVo> statisticPieVoList = signInfoMapper.statisticPie();
        return Result.success(statisticPieVoList);
    }

    @Override
    public Result statisticLine() {
        List<StatisticLine> statisticLineArray = new ArrayList<>();
        List<StatisticLine> statisticLineList = signInfoMapper.statisticLine();
        List<String> dateList = SevenDate.getSevenDate();
        for (String date : dateList) {
            boolean flag = false;
            for (StatisticLine line : statisticLineList) {
                if(date.equals(line.getDate())) {
                    flag = true;
                    statisticLineArray.add(line);
                    break;
                }
            }
            if(!flag) {
                StatisticLine statisticLine = new StatisticLine();
                statisticLine.setCount(0);
                statisticLine.setDate(date);
                statisticLineArray.add(statisticLine);
            }
        }
        return Result.success(statisticLineArray);
    }
}
